import { Body, Controller, Delete, Get, Param, ParseArrayPipe, ParseIntPipe, Patch, Query, Req } from "@nestjs/common";
import { ApiBearerAuth, ApiOperation, ApiTags } from "@nestjs/swagger";
import { Request } from "express";

import AjaxResult from "@/core/utils/AjaxResult";

import { MoveFileDto } from "../dto/move-file.dto";
import { QueryFileDto } from "../dto/query-file.dto";
import { UpdateFileDto } from "../dto/update-file.dto";
import { FileService } from "../service/file.service";

@ApiTags("文件管理")
@ApiBearerAuth()
@Controller("/admin/file")
export class FileAdminController {
    constructor(private readonly fileService: FileService) {}

    @ApiOperation({ summary: "文件分页列表" })
    @Get("/page")
    async queryFile(@Query() query: QueryFileDto, @Req() req: Request) {
        return AjaxResult.success(await this.fileService.queryFile(query, req));
    }

    @ApiOperation({ summary: "文件批量移动" })
    @Patch("/batch/move")
    async moveFile(@Body() body: MoveFileDto) {
        return AjaxResult.success(await this.fileService.moveFile(body.ids, body.dirId));
    }

    @ApiOperation({ summary: "文件信息编辑" })
    @Patch("/edit/:id")
    async updateFile(@Param("id", ParseIntPipe) id: number, @Body() updateFileDto: UpdateFileDto, @Req() req: Request) {
        return AjaxResult.success(await this.fileService.update(id, updateFileDto, req));
    }

    @ApiOperation({ summary: "文件批量删除" })
    @Delete("/batch/delete")
    async removeBatch(@Query("ids", new ParseArrayPipe({ separator: "," })) ids: string[]) {
        return AjaxResult.success(await this.fileService.removeBatch(ids.map(id => +id)));
    }
}
